/**   
* @Title: StringUtils.java
* @Package org.xaoyaoyao.cloud.common.utils
* @Description: TODO
* @author xaoyaoyao
* @date Feb 11, 2019 2:37:36 PM
* @version V1.0   
*/
package org.xaoyaoyao.cloud.common.utils;

import java.util.Collection;
import java.util.Locale;
import java.util.Map;

/**
 * @ClassName: StringUtils
 * @Description: TODO
 * @author xaoyaoyao
 * @date Feb 11, 2019 2:37:36 PM
 * 
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

	/**
	 * The constant STRING_NULL.
	 */
	private final static String STRING_NULL = "-";

	/**
	 * 匹配手机号码, 支持+86和86开头
	 */
	private static final String REGX_MOBILENUM = "^((\\+86)|(86))?(13|15|17|18|19)\\d{9}$";

	/**
	 * 匹配邮箱帐号
	 */
	private static final String REGX_EMAIL = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";

	/**
	 * @Title: getLocale
	 * @Description: 获取语言
	 * @param language
	 * @return Locale
	 */
	public static Locale getLocale(String language) {
		Locale locale = Locale.US;
		if (isBlank(language)) {
			locale = Locale.CHINA;
		} else if (language.contains("cn")) {
			locale = Locale.CHINA;
		} else if (language.contains("en") || language.contains("eu")) {
			locale = Locale.US;
		} else if (language.contains("ja") || language.contains("jp")) {
			locale = Locale.JAPAN;
		}
		return locale;
	}

	/**
	 * @Title: isMobileNumber
	 * @Description: 匹配手机号码（先支持13, 15, 17, 18，19开头的手机号码）.
	 * @param inputStr
	 * @return Boolean
	 */
	public static Boolean isMobileNumber(String inputStr) {
		return !isNull(inputStr) && inputStr.matches(REGX_MOBILENUM);
	}

	/**
	 * @Title: isNull
	 * @Description: 判断一个或多个对象是否为空
	 * @param values
	 *            可变参数, 要判断的一个或多个对象
	 * @return boolean 只有要判断的一个对象都为空则返回true, 否则返回false boolean
	 */
	public static boolean isNull(Object... values) {
		if (!isNotNullAndNotEmpty(values)) {
			return true;
		}
		for (Object value : values) {
			boolean flag;
			if (value instanceof Object[]) {
				flag = !isNotNullAndNotEmpty((Object[]) value);
			} else if (value instanceof Collection<?>) {
				flag = !isNotNullAndNotEmpty((Collection<?>) value);
			} else if (value instanceof String) {
				flag = isOEmptyOrNull(value);
			} else {
				flag = (null == value);
			}
			if (flag) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @Title: isOEmptyOrNull
	 * @Description: Is o empty or null boolean.
	 * @param o
	 * @return boolean
	 */
	private static boolean isOEmptyOrNull(Object o) {
		return o == null || isSEmptyOrNull(o.toString());
	}

	/**
	 * @Title: isSEmptyOrNull
	 * @Description: Is s empty or null boolean.
	 * @param s
	 * @return boolean
	 */
	private static boolean isSEmptyOrNull(String s) {
		return trimAndNullAsEmpty(s).length() <= 0;
	}

	/**
	 * @Title: trimAndNullAsEmpty
	 * @Description: Trim and null as empty string.
	 * @param s
	 * @return String
	 */
	private static String trimAndNullAsEmpty(String s) {
		if (s != null && !s.trim().equals(STRING_NULL)) {
			return s.trim();
		} else {
			return "";
		}
	}

	/**
	 * @Title: isNotNullAndNotEmpty
	 * @Description: 判断对象数组是否为空并且数量大于0
	 * @param value
	 * @return Boolean
	 */
	private static Boolean isNotNullAndNotEmpty(Object[] value) {
		boolean bl = false;
		if (null != value && 0 < value.length) {
			bl = true;
		}
		return bl;
	}

	/**
	 * @Title: isNotNullAndNotEmpty
	 * @Description: 判断对象集合（List,Set）是否为空并且数量大于0
	 * @param value
	 * @return Boolean
	 */
	private static Boolean isNotNullAndNotEmpty(Collection<?> value) {
		boolean bl = false;
		if (null != value && !value.isEmpty()) {
			bl = true;
		}
		return bl;
	}

	/**
	 * @Title: isEmail
	 * @Description: Is email boolean.
	 * @param str
	 * @return boolean
	 */
	public static boolean isEmail(String str) {
		boolean bl = true;
		if (isSEmptyOrNull(str) || !str.matches(REGX_EMAIL)) {
			bl = false;
		}
		return bl;
	}

	/**
	 * @Title: isEmpty
	 * @Description: 判断对象是否Empty(null或元素为0) 实用于对如下对象做判断:String Collection及其子类
	 *               Map及其子类
	 * @param pObj
	 *            待检查对象
	 * @return boolean 返回的布尔值
	 */
	@SuppressWarnings("rawtypes")
	public static boolean isEmpty(Object pObj) {
		if (pObj == null) {
			return true;
		}
		if (pObj == "") {
			return true;
		}
		if (pObj instanceof String) {
			return ((String) pObj).length() == 0;
		} else if (pObj instanceof Collection) {
			return ((Collection) pObj).isEmpty();
		} else if (pObj instanceof Map) {
			return ((Map) pObj).size() == 0;
		}
		return false;
	}

	/**
	 * @Title: isNotEmpty
	 * @Description: 判断对象是否为NotEmpty(!null或元素大于0) 实用于对如下对象做判断:String
	 *               Collection及其子类 Map及其子类
	 * @param pObj
	 *            待检查对象
	 * @return boolean 返回的布尔值
	 */
	@SuppressWarnings("rawtypes")
	public static boolean isNotEmpty(Object pObj) {
		if (pObj == null) {
			return false;
		}
		if (pObj == "") {
			return false;
		}
		if (pObj instanceof String) {
			return ((String) pObj).length() != 0;
		} else if (pObj instanceof Collection) {
			return !((Collection) pObj).isEmpty();
		} else if (pObj instanceof Map) {
			return ((Map) pObj).size() != 0;
		}
		return true;
	}
}
